package hong.jun.time;
import java.util.concurrent.*;
/**
 * 说明:
 * Created on 2023/10/25 14:18
 *
 * @author yanghongjun <1737619387@qq.com>
 */
public abstract class Timer implements Runnable {
    /**
     * 心跳名
     */
    private final String timerName;
    /**
     * 第一次延时多久执行
     */
    private final long initialDelayMS;

    /**
     * 固定频率
     */
    private final long periodMS;

    protected Timer(String timerName, long initialdelaymS, long periodmS) {
        this.timerName = timerName;
        this.initialDelayMS = initialdelaymS;
        this.periodMS = periodmS;
    }


    @Override
    public void run() {
        try {
            action();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public void start() {
        TimeManger.getInstance().getExecutorService().scheduleAtFixedRate(this, getInitialDelayMS(), getPeriodMS(), TimeUnit.MILLISECONDS);
    }

    protected abstract void action();

    public String getTimerName() {
        return timerName;
    }

    public long getInitialDelayMS() {
        return initialDelayMS;
    }

    public long getPeriodMS() {
        return periodMS;
    }
}
